﻿addonName, AR = ...

UIErrorsFrame:UnregisterEvent("UI_ERROR_MESSAGE")

AR_Settings = {
	MinimapPos = 45,
	IsVisible = false,
	ROTATION_STARTED = false,
	INTERRUPTER_STARTED = false
}

function AR_MinimapButton_Reposition()
	AR_MinimapButton:SetPoint("TOPLEFT","Minimap","TOPLEFT",52-(80*cos(AR_Settings.MinimapPos)),(80*sin(AR_Settings.MinimapPos))-52)
end

function AR_MinimapButton_DraggingFrame_OnUpdate()
	
	local xpos,ypos = GetCursorPosition()
	local xmin,ymin = Minimap:GetLeft(), Minimap:GetBottom()
	
	xpos = xmin-xpos/UIParent:GetScale()+70
	ypos = ypos/UIParent:GetScale()-ymin-70
	
	AR_Settings.MinimapPos = math.deg(math.atan2(ypos,xpos))
	AR_MinimapButton_Reposition()
	
end

function AR_MinimapButton_OnClick()
	AR_Settings.IsVisible = not AR_Settings.IsVisible
	if AR_Settings.IsVisible then AR_frame:Show() else AR_frame:Hide() end
end

function AR_frame_OnLoad()
	
	AR_frame:SetSize(110, 60)
	AR_frame:SetPoint("CENTER", -200, 0)
	AR_frame:SetBackdrop({ bgFile = "Interface\\ChatFrame\\ChatFrameBackground", edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tileSize = 12, edgeSize = 12, insets = {left=3, right=3, top=3, bottom=3}, })
	AR_frame:SetBackdropColor(.1, .2, .3, 1)
	AR_frame:SetBackdropBorderColor(.4, .4, .4, 1)
	AR_frame:EnableMouse(true)
	AR_frame:SetMovable(true)
	AR_frame:SetUserPlaced(true)
	AR_frame:SetClampedToScreen(true)
	AR_frame:SetScript("OnMouseDown", function(self) self:StartMoving() end)
	AR_frame:SetScript("OnMouseUp", function(self) self:StopMovingOrSizing() end)
	AR_frame:SetScript("OnHide", function(self) AR_Settings.IsVisible = false end)
	AR_frame:SetScript("OnShow", function(self) AR_Settings.IsVisible = true end)
	AR_frame:SetFrameStrata("DIALOG")
	AR_frame:Hide()
	
	_G[Rotation_checkbox:GetName()..'Text']:SetText('Rotation.')
	Rotation_checkbox:SetPoint('TOPLEFT', AR_frame, 10, -10)
	Rotation_checkbox:SetSize(20, 20)
	Rotation_checkbox:SetHitRectInsets(0, -20, 0, 0)
	Rotation_checkbox:SetChecked(ROTATION_STARTED)
	
	_G[Interrupter_checkbox:GetName()..'Text']:SetText('Interrupter.')
	Interrupter_checkbox:SetPoint('TOPLEFT', AR_frame, 10, -30)
	Interrupter_checkbox:SetSize(20, 20)
	Interrupter_checkbox:SetHitRectInsets(0, -20, 0, 0)
	Interrupter_checkbox:SetChecked(INTERRUPTER_STARTED)
	
	BindingRotationButton:SetPoint('TOPLEFT', AR_frame, 40, -10)
	BindingRotationButton:SetSize(60, 20)
	
	BindingInterrupterButton:SetPoint('TOPLEFT', AR_frame, 40, -30)
	BindingInterrupterButton:SetSize(60, 20)
	
end

function AR_Rotation_checkbox_OnClick()
	AR_Settings.ROTATION_STARTED = not AR_Settings.ROTATION_STARTED
	Rotation_checkbox:SetChecked(AR_Settings.ROTATION_STARTED)
	if AR_Settings.ROTATION_STARTED then
		print(" ")
		print("|CFF1CB619Rotation|R : |CFFFE8A0EStarted.|R")
	else
		print(" ")
		print("|CFF1CB619Rotation|R : |CFFFE8A0EStopped.|R")
	end
end

function AR_Interrupter_checkbox_OnClick()
	AR_Settings.INTERRUPTER_STARTED = not AR_Settings.INTERRUPTER_STARTED
	Interrupter_checkbox:SetChecked(AR_Settings.INTERRUPTER_STARTED)
	if AR_Settings.INTERRUPTER_STARTED then
		print(" ")
		print("|CFF1CB619Interrupter|R : |CFFFE8A0EStarted.|R")
	else
		print(" ")
		print("|CFF1CB619Interrupter|R : |CFFFE8A0EStopped.|R")
	end
end

local _G = _G
local Rotation_Starter_Frame = _G['Rotation_Starter']
if Rotation_Starter_Frame == nil then Rotation_Starter_Frame = CreateFrame('frame', 'Rotation_Starter', UIParent) end

Rotation_Starter_Frame:SetScript('OnUpdate', function(self, elapsed)
	RegisterMovement(elapsed)
	local throttle = 1 / GetFramerate()
	self.st = elapsed + (self.st or 0)
	if self.st > throttle then
		if AR_Settings.INTERRUPTER_STARTED then
			for i = 1, #InterrupterTargets do
				Interrupter(InterrupterTargets[i])
			end
		end
		if AR_Settings.ROTATION_STARTED then
			local MyClass = select(2, UnitClass('player')):lower()
			AR[MyClass].ROTATION()
		end
		self.st = 0
	end
end)